<template>
  <div style="display: flex; justify-content: center; align-items: center;">
    <a-card>
      <a-form :model="config" :style="{width:'600px'}" auto-label-width>
        <a-form-item field="GEWE_TOKEN" label="GEWE平台token">
          <a-input v-model="config.GEWE_TOKEN" disabled />
        </a-form-item>
        <a-form-item field="LLK_TOKEN" label="领路客Tokne">
          <a-input v-model="config.LLK_TOKEN" disabled />
        </a-form-item>
        <a-form-item field="CALLBACK_URL" label="回调地址">
          <a-input v-model="config.CALLBACK_URL" disabled />
        </a-form-item>
        <a-form-item>
          <a-button html-type="submit" style="margin-right: 20px" @click="updateGeweToken">更新GEWE平台token</a-button>
          <a-button html-type="submit" style="margin-right: 20px" @click="updateLlkTokenVisible=true">更新领路客Tokne
          </a-button>
          <a-button html-type="submit" style="margin-right: 20px" @click="getDefaultCallBack">更新回调地址</a-button>
        </a-form-item>
      </a-form>
    </a-card>
    <a-modal v-model:visible="updateLlkTokenVisible" @ok="updateLlkTokenButton" @cancel="cancelUpdateLlkToken">
      <template #title>
        更新LLK Token
      </template>
      <div>
        <a-input v-model="updateLlkToken" placeholder="请输入最新LLK Token" />
      </div>
    </a-modal>

    <a-modal v-model:visible="updateCallbackUrlvisible" @ok="updateCallbackUrlButton" @cancel="cancelCallbackUrl">
      <template #title>
        更新回调地址
      </template>
      <div>
        <a-input v-model="callBackUrl" placeholder="请输入回调地址" />
      </div>
    </a-modal>
  </div>
</template>


<script lang="ts" setup>

  import { onMounted, reactive, ref } from "vue";
  import { SysConfControllerService } from "../../generated";

  const config = reactive({
    "GEWE_TOKEN": "",
    "LLK_TOKEN": "",
    "CALLBACK_URL": ""
  });

  const updateLlkTokenVisible = ref(false);
  const updateLlkToken = ref("");

  const updateCallbackUrlvisible = ref(false);
  const callBackUrl = ref("");
  const getConfig = async () => {
    const res = await SysConfControllerService.getConfigUsingPost();
    if (res.code == 0) {
      config.GEWE_TOKEN = res.data.GEWE_TOKEN;
      config.LLK_TOKEN = res.data.LLK_TOKEN;
      config.CALLBACK_URL = res.data.CALLBACK_URL;
    }

  };

  onMounted(() => {
    getConfig();
  });

  const updateGeweToken = async () => {
    const res = await SysConfControllerService.updateGeweTokenUsingPost();
    console.log(res);
    if (res.code == 0) {
      alert("更新Gewe Token成功");
    } else {
      alert(res.message);
    }
    getConfig();
  };

  const updateLlkTokenButton = async () => {
    const res = await SysConfControllerService.setLlkTokenUsingPost(updateLlkToken.value);
    updateLlkTokenVisible.value = false;
    if (res.code == 0) {
      alert("更新LLK Token成功");
    } else {
      alert(res.message);
    }
    getConfig();
  };
  const cancelUpdateLlkToken = () => {
    updateLlkTokenVisible.value = false;
  };

  const updateCallbackUrlButton = async () => {
    const res = await SysConfControllerService.setCallbackUsingPost(callBackUrl.value);
    if (res.code == 0) {
      alert("更新回调地址成功");
    } else {
      alert(res.message);
    }
    updateLlkTokenVisible.value = false;
    getConfig();
  };
  const cancelCallbackUrl = () => {
    updateLlkTokenVisible.value = false;
  };

  //获取默认地址
  const getDefaultCallBack = async () => {
    const res = await SysConfControllerService.getdefaultCallbackUrlUsingPost();
    if (res.code == 0) {
      callBackUrl.value = res.data;
    }
    console.log(callBackUrl.value);
    updateCallbackUrlvisible.value = true;
  };

</script>

